home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivwparser.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  7.7 KB  |  365 lines

  1. unit IvWParser;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, SysUtils,
  9.   IvDictio;
  10.  
  11. type
  12.   TIvWideParser = class(TObject)
  13.   protected
  14.     FPosition: Integer;
  15.     FMaxPosition: Integer;
  16.     FSeparator: WideChar;
  17.     FValue: TIvWideString;
  18.     FConvert: Boolean;
  19.     FCodePage: Integer;
  20.  
  21.     procedure SetValue(const value: TIvWideString);
  22.  
  23.     function GetCurrentValue: TIvWideString;
  24.  
  25.   public
  26.     constructor Create;
  27.     constructor CreateValue(const value: TIvWideString; separator: WideChar);
  28.  
  29.     function Eol: Boolean;
  30.  
  31.     function GetString: TIvWideString;
  32.     function GetChar: WideChar;
  33.     function GetInteger: Integer;
  34.     function GetFloat: Double;
  35.     function GetBoolean: Boolean;
  36.  
  37.     function GetAnsiString: String;
  38.     function GetAnsiChar: Char;
  39.  
  40.     function GetCharDef(defaultValue: WideChar): WideChar;
  41.     function GetIntegerDef(defaultValue: Longint): Longint;
  42.     function GetFloatDef(defaultValue: Double): Double;
  43.     function GetBooleanDef(defaultValue: Boolean): Boolean;
  44.  
  45.     class function CodeStr(const str: TIvWideString): TIvWideString;
  46.     class function DecodeStr(const str: TIvWideString): TIvWideString;
  47.  
  48.     property Position: Integer read FPosition;
  49.     property Separator: WideChar read FSeparator write FSeparator;
  50.     property CurrentValue: TIvWideString read GetCurrentValue;
  51.     property Value: TIvWideString read FValue write SetValue;
  52.     property Convert: Boolean read FConvert write FConvert;
  53.     property CodePage: Integer read FCodePage write FCodePage;
  54.   end;
  55.  
  56. implementation
  57.  
  58. {$IFDEF IVANSI}
  59. var
  60.   commonString: PWideChar;
  61. {$ENDIF}
  62.  
  63. constructor TIvWideParser.Create;
  64. begin
  65.   inherited Create;
  66.   FConvert := False;
  67.   FPosition := 0;
  68.   FSeparator := #9;
  69.   SetValue('');
  70. end;
  71.  
  72. constructor TIvWideParser.CreateValue(
  73.   const value: TIvWideString;
  74.   separator: WideChar);
  75. begin
  76.   inherited Create;
  77.   FConvert := False;
  78.   FPosition := 0;
  79.   FSeparator := separator;
  80.   SetValue(value);
  81. end;
  82.  
  83. procedure TIvWideParser.SetValue(const value: TIvWideString);
  84. begin
  85.   FValue := value;
  86. {$IFDEF IVWIDE}
  87.   FMaxPosition := Length(FValue) + 1;
  88.   FPosition := 1;
  89. {$ELSE}
  90.   FMaxPosition := SysStringLen(FValue);
  91.   FPosition := 0;
  92. {$ENDIF}
  93. end;
  94.  
  95. function TIvWideParser.Eol: Boolean;
  96. begin
  97.   Result := FPosition >= FMaxPosition;
  98. end;
  99.  
  100. class function TIvWideParser.CodeStr(const str: TIvWideString): TIvWideString;
  101. var
  102.   c: WideChar;
  103.   i: Integer;
  104. {$IFDEF IVANSI}
  105.   len, index: Integer;
  106. {$ENDIF}
  107. begin
  108. {$IFDEF IVWIDE}
  109.   Result := '';
  110.   for i := 1 to Length(str) do
  111.   begin
  112.     c := str[i];
  113.     case c of
  114.       #9: Result := Result + '#T';
  115.       #10: Result := Result + '#C';
  116.       #13: Result := Result + '#L';
  117.     else
  118.       Result := Result + WideString(c);
  119.     end;
  120.   end;
  121. {$ELSE}
  122.   len := SysStringLen(str);
  123.   Result := SysAllocStringLen(nil, 2*len + 1);
  124.   for i := 0 to len do
  125.   begin
  126.     c := str[i];
  127.     case c of
  128.       WideChar(#9):
  129.       begin
  130.         Result[index] := '#';
  131.         Inc(index);
  132.         Result[index] := 'T';
  133.       end;
  134.  
  135.       WideChar(#10):
  136.       begin
  137.         Result[index] := '#';
  138.         Inc(index);
  139.         Result[index] := 'C';
  140.       end;
  141.  
  142.       WideChar(#13):
  143.       begin
  144.         Result[index] := '#';
  145.         Inc(index);
  146.         Result[index] := 'L';
  147.       end;
  148.     else
  149.       Result[index] := c;
  150.     end;
  151.     Inc(index);
  152.   end;
  153.  
  154.   SysFreeString(commonString);
  155.   commonString := Result;
  156. {$ENDIF}
  157. end;
  158.  
  159. class function TIvWideParser.DecodeStr(const str: TIvWideString): TIvWideString;
  160. var
  161.   c: WideChar;
  162.   len, src, dest: Integer;
  163. begin
  164. {$IFDEF IVWIDE}
  165.   len := Length(str);
  166.   SetLength(Result, len);
  167.   dest := 1;
  168.   src := 1;
  169.   while src <= len do
  170.   begin
  171.     c := str[src];
  172.     if c = '#' then
  173.     begin
  174.       Inc(src);
  175.       if src <= len then
  176.       begin
  177.         c := str[src];
  178.         case c of
  179.           '#': c := '#';
  180.           'T': c := #9;
  181.           'L': c := #13;
  182.           'C': c := #10;
  183.         else
  184.           Result[dest] := '#';
  185.           Inc(dest);
  186.         end;
  187.       end;
  188.     end;
  189.  
  190.     Result[dest] := c;
  191.     Inc(src);
  192.     Inc(dest);
  193.   end;
  194.  
  195.   // Sets the string length to actual length
  196.  
  197.   SetLength(Result, dest - 1);
  198. {$ELSE}
  199.   // Delphi 2 and C++Builder 1 do not support coded Unicode text dictionaries!
  200.  
  201.   Result := str;
  202. {$ENDIF}
  203. end;
  204.  
  205. function TIvWideParser.GetCurrentValue: TIvWideString;
  206. var
  207.   start, pos: Integer;
  208. begin
  209. {$IFDEF IVWIDE}
  210.   if FValue = '' then
  211.     Result := ''
  212.   else
  213.   begin
  214.     pos := FPosition;
  215.     start := FPosition;
  216.     while (pos < FMaxPosition) and (FValue[pos] <> FSeparator) do
  217.       Inc(pos);
  218.  
  219.     if pos = start then
  220.       Result := ''
  221.     else
  222.       Result := Copy(FValue, start, pos - start);
  223.   end;
  224. {$ELSE}
  225.   if SysStringLen(FValue) = 0 then
  226.     Result := ''
  227.   else
  228.   begin
  229.     pos := FPosition;
  230.     start := FPosition;
  231.     while (pos < FMaxPosition) and (FValue[pos] <> FSeparator) do
  232.       Inc(pos);
  233.  
  234.     if pos = start then
  235.       Result := ''
  236.     else
  237.       Result := SysAllocStringLen(Pointer(@FValue[start]), pos - start);
  238.   end;
  239.  
  240.   SysFreeString(commonString);
  241.   commonString := Result;
  242. {$ENDIF}
  243. end;
  244.  
  245. function TIvWideParser.GetString: TIvWideString;
  246. var
  247.   start: Integer;
  248. begin
  249. {$IFDEF IVWIDE}
  250.   if FValue = '' then
  251.     Result := ''
  252.   else
  253.   begin
  254.     start := FPosition;
  255.     while (FPosition < FMaxPosition) and (FValue[FPosition] <> FSeparator) do
  256.       Inc(FPosition);
  257.  
  258.     if FPosition = start then
  259.       Result := ''
  260.     else
  261.       Result := Copy(FValue, start, FPosition - start);
  262.     Inc(FPosition);
  263.  
  264.     if FConvert and (Pos('#', Result) > 0) then
  265.       Result := DecodeStr(Result);
  266.   end;
  267. {$ELSE}
  268.   if SysStringLen(FValue) = 0 then
  269.     Result := ''
  270.   else
  271.   begin
  272.     start := FPosition;
  273.     while (FPosition < FMaxPosition) and (FValue[FPosition] <> FSeparator) do
  274.       Inc(FPosition);
  275.  
  276.     if FPosition = start then
  277.       Result := ''
  278.     else
  279.       Result := SysAllocStringLen(Pointer(@FValue[start]), FPosition - start);
  280.     Inc(FPosition);
  281.   end;
  282.  
  283.   SysFreeString(commonString);
  284.   commonString := Result;
  285. {$ENDIF}
  286. end;
  287.  
  288. function TIvWideParser.GetAnsiString: String;
  289. begin
  290.   Result := IvWStrToStr(GetString, FCodePage);
  291. end;
  292.  
  293. function TIvWideParser.GetChar: WideChar;
  294. begin
  295.   Result := GetString[1];
  296. end;
  297.  
  298. function TIvWideParser.GetCharDef(defaultValue: WideChar): WideChar;
  299. begin
  300.   if Eol then
  301.     Result := defaultValue
  302.   else
  303.     Result := GetChar;
  304. end;
  305.  
  306. function TIvWideParser.GetAnsiChar: Char;
  307. var
  308.   str: String;
  309. begin
  310.   str := IvWStrToStr(GetString, FCodePage);
  311.   Result := str[1];
  312. end;
  313.  
  314. function TIvWideParser.GetInteger: Integer;
  315. begin
  316.   Result := StrToInt(GetAnsiString);
  317. end;
  318.  
  319. function TIvWideParser.GetIntegerDef(defaultValue: Longint): Longint;
  320. begin
  321.   Result := StrToIntDef(GetAnsiString, defaultValue);
  322. end;
  323.  
  324. function TIvWideParser.GetFloat: Double;
  325. begin
  326.   Result := StrToFloat(GetAnsiString);
  327. end;
  328.  
  329. function TIvWideParser.GetFloatDef(defaultValue: Double): Double;
  330. begin
  331.   if Eol then
  332.     Result := defaultValue
  333.   else
  334.     Result := GetFloat;
  335. end;
  336.  
  337. function TIvWideParser.GetBoolean: Boolean;
  338. var
  339.   str: String;
  340. begin
  341.   str := GetAnsiString;
  342.   if (str = '0') or (CompareText(str, 'false') = 0) or (CompareText(str, 'no') = 0) then
  343.     Result := False
  344.   else if (str = '1') or (CompareText(str, 'true') = 0) or (CompareText(str, 'yes') = 0) then
  345.     Result := True
  346.   else
  347.     raise Exception.Create(str + ' is not a boolean value');
  348. end;
  349.  
  350. function TIvWideParser.GetBooleanDef(defaultValue: Boolean): Boolean;
  351. begin
  352.   if Eol then
  353.     Result := defaultValue
  354.   else
  355.     Result := GetBoolean;
  356. end;
  357.  
  358. initialization
  359. {$IFDEF IVANSI}
  360.   commonString := nil;
  361. finalization
  362.   SysFreeString(commonString);
  363. {$ENDIF}
  364. end.
  365.